﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title></title>
    <link href="~/lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
    <link href="~/lib/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
    <link href="~/lib/ligerUI/skins/Gray/css/all.css" rel="stylesheet" type="text/css" />
    <link href="~/Content/css/common.css" rel="stylesheet" type="text/css" />
    <script src="~/lib/jquery/jquery-1.9.0.min.js" type="text/javascript"></script>
    <script src="~/lib/ligerUI/js/ligerui.all.js"></script>
    <script src="~/Scripts/common.js"></script>
    <style>
        .l-layout-header {
            background: none;
        }
    </style>
</head>
<body style="overflow: hidden;">
    <div class="searchbar">
        <input id="btnSave" type="button" value="保存" class="l-button" />
    </div>
    <div id="layout">
        <div position="left" title="角色列表">
            <div id="treediv" style="width: 250px; height: 100%; margin: -1px; float: left; border: 1px solid #ccc; overflow: auto; padding-top: 5px;">
                <ul id="treeRole"></ul>
            </div>
        </div>
        <div position="center" title="菜单列表">
            <div id="maingrid"></div>
        </div>
    </div>
</body>
</html>
<script>
    var manager = null;
    var treemanager = null;
    $(function () {
        $("#layout").ligerLayout({ leftWidth: 150, allowLeftResize: false, allowLeftCollapse: false, space: 4, heightDiff: 1 });

        treemanager = $("#treeRole").ligerTree({
            url: '/Sysbase/Role/GetRoleList?ram=' + Math.random(),
            textFieldName: 'RoleName',
            idFieldName: 'RoleCode',
            checkbox: false,
            itemopen: false,
            onSelect: search,
            onBeforeCancelSelect: function () { return false; },
            onSuccess: function () {
                $(".l-first").click();
            }
        });

        $('#btnSearch').click(function () {
            search();
        });

        $('#btnSave').click(function () {
            save();
        });
    });

    //加载列表
    function initTable(queryData, pageNumber) {
        manager = $("#maingrid").ligerGrid({
            url: '/Sysbase/Power/Search?ram=' + Math.random(),
            parms: queryData,
            page: pageNumber,
            columns: [
                { display: '菜单名称', name: 'MenuName', align: 'left', width: 400 }
            ],
            rowid: "MenuId",
            checkbox: true,
            usePager: false,
            rownumbers: false,
            allowHideColumn: false,
            tree: {
                columnName: 'MenuName',
                idField: 'MenuId',
                parentIDField: 'ParentId'
            },
            height: '100%',
            heightDiff: 25,
            onCheckRow: onCheckRow,
            isChecked: isChecked
        });
        GridNoData($("#maingrid"));
    }

    //搜索
    function search(pageNumber) {
        var note = treemanager.getSelected();
        if (manager == null) {
            var queryData = {
                powerRole: note == null ? "" : note.data.RoleCode
            }
            initTable(queryData, pageNumber);
        }
        else {
            manager.setParm("powerRole", note == null ? "" : note.data.RoleCode);
            manager.loadData();
        }
    }

    function onCheckRow(checked, data) {
        if (checked) {
            while (data) {
                data = manager.getParent(data);
                if (data) manager.select(data);
            }
        }
    }

    function isChecked(data) {
        if (data.Checked == 1) {
            return true;
        }
        else {
            return false;
        }
    }

    function save() {
        var powerList = [];
        var note = treemanager.getSelected();
        var powerRole = note == null ? "" : note.data.RoleCode;
        var rows = manager.getCheckedRows();
        $(rows).each(function () {
            powerList.push({ MenuId: this.MenuId, PowerRole: powerRole });
        });

        $.ajax({
            url: "/Sysbase/Power/Save",
            type: "post",
            data: JSON.stringify({ powerRole: powerRole, powList: powerList }),
            type: 'post',
            contentType: 'application/json;charset=utf-8',
            success: function (r) {
                var r = $.parseJSON(r);
                if (r.Result == 1) {
                    $.ligerDialog.success("保存成功");
                }
                else if (r.Result == 0) {
                    $.ligerDialog.error(r.Body);
                }
                else {
                    $.ligerDialog.error("程序错误");
                }
            },
            error: function () {
                $.ligerDialog.error("程序错误");
            }
        });
    }
</script>